package com.woniushop.dao.imple;

import java.util.List;

import com.woniushop.dao.BaseDAO;
import com.woniushop.dao.OrderFormDAO;
import com.woniushop.entity.OrderForm;

/**
 * 订单DAO的实现类
 * 
 * @author 崔新雄
 *
 */
public class OrderFormDAOImple implements OrderFormDAO {

	private BaseDAO<OrderForm> baseDAO = new BaseDAO<OrderForm>();

	/**
	 * 将orderForm对象加入数据库中
	 * 
	 * @param orderForm
	 */
	@Override
	public void add(OrderForm orderForm) {
		String sql = "insert into orderform(uid,aid,odate,ostate,ono) values(?,?,?,?,?)";
		Object[] objs = { orderForm.getUid(), orderForm.getAid(), orderForm.getOdate(), orderForm.getOstate(),
				orderForm.getOno() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据订单id删除数据库中对应的订单数据
	 * 
	 * @param oid
	 */
	@Override
	public void delete(Integer oid) {
		String sql = "delete from orderform where oid=?";
		Object[] objs = { oid };
		baseDAO.update(sql, objs);
	}

	/**
	 * 修改对应的订单数据
	 * 
	 * @param orderForm
	 */
	@Override
	public void update(OrderForm orderForm) {
		String sql = "update orderform set uid=?,aid=?,odate=?,ostate=?,ono=? where oid=?";
		Object[] objs = { orderForm.getUid(), orderForm.getAid(), orderForm.getOdate(), orderForm.getOstate(),
				orderForm.getOno(), orderForm.getOid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据对应的订单id查找订单数据
	 * 
	 * @param oid
	 * @return
	 */
	@Override
	public OrderForm findOne(Integer oid) {
		String sql = "select o.*,u.uname,u.uaccount,a.aname,a.acode,a.aphone,a.ainfo from orderform o,user u,address a where o.uid=u.uid and o.aid=a.aid and oid=?";
		Object[] objs = { oid };
		List<OrderForm> list = baseDAO.select(sql, OrderForm.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据对应的订单编号查找订单数据
	 * 
	 * @param ono
	 * @return
	 */
	@Override
	public OrderForm findOne(String ono) {
		String sql = "select o.*,u.uname,u.uaccount,a.aname,a.acode,a.aphone,a.ainfo from orderform o,user u,address a where o.uid=u.uid and o.aid=a.aid and ono=?";
		Object[] objs = { ono };
		List<OrderForm> list = baseDAO.select(sql, OrderForm.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 多条件分页查询订单数据
	 * 
	 * @param page
	 * @param pageSize
	 * @param orderForm
	 * @return
	 */
	@Override
	public List<OrderForm> findByPage(Integer page, Integer pageSize, OrderForm orderForm) {
		String sql = "select o.*,u.uname,u.uaccount,a.aname,a.acode,a.aphone,a.ainfo from orderform o,user u,address a where o.uid=u.uid and o.aid=a.aid";
		sql = finishSQL(orderForm, sql);
		sql += " order by o.ostate asc limit ?,?";
		return baseDAO.select(sql, OrderForm.class, (page - 1) * pageSize, pageSize);
	}

	/**
	 * 查找符合条件的订单数量
	 * 
	 * @param orderForm
	 * @return
	 */
	@Override
	public Integer count(OrderForm orderForm) {
		String sql = "select count(*) from orderform o,user u,address a where o.uid=u.uid and o.aid=a.aid";
		sql = finishSQL(orderForm, sql);
		return baseDAO.count(sql);
	}

	/**
	 * 完成多条件查询的SQl语句
	 * 
	 * @param orderForm
	 * @param sql
	 * @return
	 */
	private String finishSQL(OrderForm orderForm, String sql) {
		String ono = orderForm.getOno();
		if (ono != null && !ono.trim().equals("")) {
			sql += " and o.ono like '%" + ono + "%'";
		}
		Integer aid = orderForm.getAid();
		if (aid != null) {
			sql += " and  o.aid='" + aid + "'";
		}
		Integer ostate = orderForm.getOstate();
		if (ostate != null && ostate > 0) {
			sql += " and  o.ostate='" + ostate + "'";
		} else if (ostate != null && ostate == 0) {// ostate == 0表示查所有可用订单
			sql += " and  o.ostate!='1'";
		}
		Integer uid = orderForm.getUid();
		if (uid != null) {
			sql += " and  o.uid='" + uid + "'";
		}
		return sql;
	}

}
